﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;

namespace ECS.Framework
{
    public class PCPrint : System.Drawing.Printing.PrintDocument
    {
        #region private properties
        /// <summary>
        /// Property variable for the Font the user wishes to use
        /// </summary>
        private Font _font;
        /// <summary>
        /// Property variable for the text to be printed
        /// </summary>
        private string _text;
        #endregion

        #region public properties
        /// <summary>
        /// Property to hold the text that is to be printed
        /// </summary>
        public string TextToPrint
        {
            get { return _text; }
            set { _text = value; }
        }
        /// <summary>
        /// Property to hold the font the users wishes to use
        /// </summary>
        public Font PrinterFont
        {
            // Allows the user to override the default font
            get { return _font; }
            set { _font = value; }
        }

        #endregion
        /// <summary>
        /// static variable to hold the current character
        /// we're currently dealing with.
        /// </summary>
        static int curChar;

        #region constructor


        /// <summary>
        /// Empty constructor
        /// </summary>
        public PCPrint()
            : base()
        {
            _text = string.Empty;
        }
        /// <summary>
        /// Constructor to initialize our printing object
        /// and the text it's supposed to be printing
        /// </summary>
        /// <param name="str"></param>
        public PCPrint(string str)
            : base()
        {
            _text = str;
        }
        #endregion

        #region private methods
        protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
        {
            //run base code
            base.OnBeginPrint(e);
            //Check to see if the user provided a font

            if (_font == null)
            {
                //Create the font we need
                _font = new Font("Times New Roman", 8);
            }
        }
        /// <summary>
        /// Override the default OnPrintPage method of the PrintDocument
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
        {
            int printHeight = 0;
            int printWidth = 0;
            int leftMargin = 0;
            int rightMargin = 0;
            Int32 lines = 0;
            Int32 chars = 0;

            // Run base code
            base.OnPrintPage(e);

            //Set print area size and margins

            printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
            printWidth = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
            leftMargin = base.DefaultPageSettings.Margins.Left;  //X
            rightMargin = base.DefaultPageSettings.Margins.Top;  //Y


            //Check if the user selected to print in Landscape mode
            //if they did then we need to swap height/width parameters
            if (base.DefaultPageSettings.Landscape)
            {
                int tmp;
                tmp = printHeight;
                printHeight = printWidth;
                printWidth = tmp;
            }

            //Now we need to determine the total number of lines
            //we're going to be printing
            Int32 numLines = (int)printHeight / PrinterFont.Height;

            //Create a rectangle printing are for our document
            RectangleF printArea = new RectangleF(leftMargin, rightMargin, printWidth, printHeight);
            //Use the StringFormat class for the text layout of our document
            StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
            //Fit as many characters as we can into the print area     
            e.Graphics.MeasureString(_text.Substring(RemoveZeros(curChar)), PrinterFont, new SizeF(printWidth, printHeight), format, out chars, out lines);
            //Print the page
            e.Graphics.DrawString(_text.Substring(RemoveZeros(curChar)), PrinterFont, Brushes.Black, printArea, format);
            //Increase current char count
            curChar += chars;
            //Detemine if there is more text to print, if
            //there is the tell the printer there is more coming
            if (curChar < _text.Length)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
                curChar = 0;
            }

        }
        #endregion

        #region public methods
        /// <summary>
        /// Function to replace any zeros in the size to a 1
        /// Zero's will mess up the printing area
        /// </summary>
        /// <param name=value>Value to check</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public int RemoveZeros(int value)
        {
            //Check the value passed into the function,
            //if the value is a 0 (zero) then return a 1,
            //otherwise return the value passed in
            switch (value)
            {
                case 0:
                    return 1;
                default:
                    return value;
            }
        }


        #endregion

        #region Sample code
        //public void PrintDocument()
        //{
        //    //Create an instance of our printer class
        //    PCPrint printer = new PCPrint();
        //    //Set the font we want to use
        //    printer.PrinterFont = new Font("Verdana", 10);
        //    //Set the TextToPrint property
        //    printer.TextToPrint = Textbox1.Text;
        //    //Issue print command
        //    printer.Print();
        //}

        #endregion
    }
}
